package com.nowcoder.community.config;

import com.nowcoder.community.quartz.AlphaJob;
import com.nowcoder.community.quartz.PostScoreRefreshJob;
import org.quartz.JobDataMap;
import org.quartz.JobDetail;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.quartz.JobDetailFactoryBean;
import org.springframework.scheduling.quartz.SimpleTriggerFactoryBean;

// 配置（仅在第一次被读取到） -> 信息被初始化到数据库 -> quartz访问数据库去调用该任务，不在访问配置文件
//需要配置jobdetail和trigger
@Configuration
public class QuartzConfig {
    //FactoryBean和之前一开始学的IOC 学到的 BeanFactory有本质区别；BeanFactory是整个IOC容器的顶层接口
    // FactoryBean主要目的是 简化Bean的实例化过程，因为有的Bean实例化过程比较复杂:
    // 1.通过FactoryBean封装Bean的实例化过程.
    // 2.将FactoryBean装配到Spring容器里.
    // 3.将FactoryBean注入给其他的Bean.
    // 4.该Bean得到的是FactoryBean所管理的对象实例.
// 配置JobDetail
//@Bean
    public JobDetailFactoryBean alphaJobDetail() {//Bean的名字是alphaJobDetail。初始化该bean，想当于将其装配到容器中
        JobDetailFactoryBean factoryBean = new JobDetailFactoryBean();//实例化对象
        factoryBean.setJobClass(AlphaJob.class);
        factoryBean.setName("alphaJob");//声明job任务的名字
        factoryBean.setGroup("alphaJobGroup");//声明任务的组
        factoryBean.setDurability(true);//声明任务是否长久保存，哪怕任务不再运行。连触发器都没有了，也会一直报存
        factoryBean.setRequestsRecovery(true);//声明任务是否可恢复
        return factoryBean;
    }

    // 配置Trigger(SimpleTriggerFactoryBean比较简单，每十天要做...； CronTriggerFactoryBean复杂，每个月月底前两天要做...)
// @Bean
    public SimpleTriggerFactoryBean alphaTrigger(JobDetail alphaJobDetail) {//Trigger依赖于JobDetail，因此需要读取
        SimpleTriggerFactoryBean factoryBean = new SimpleTriggerFactoryBean();
        factoryBean.setJobDetail(alphaJobDetail);
        factoryBean.setName("alphaTrigger");
        factoryBean.setGroup("alphaTriggerGroup");
        factoryBean.setRepeatInterval(3000);//多长时间执行一次任务
        factoryBean.setJobDataMap(new JobDataMap());//Trigger底层需要存储一些状态，新建JobDataMap对象来存储
        return factoryBean;
    }
//刷新帖子分数的任务
    @Bean
    public JobDetailFactoryBean postScoreRefreshJobDetail() {//Bean的名字是alphaJobDetail。初始化该bean，想当于将其装配到容器中
        JobDetailFactoryBean factoryBean = new JobDetailFactoryBean();//实例化对象
        factoryBean.setJobClass(PostScoreRefreshJob.class);
        factoryBean.setName("postScoreRefreshJob");//声明job任务的名字
        factoryBean.setGroup("communityJobGroup");//声明任务的组
        factoryBean.setDurability(true);//声明任务是否长久保存，哪怕任务不再运行。连触发器都没有了，也会一直报存
        factoryBean.setRequestsRecovery(true);//声明任务是否可恢复
        return factoryBean;
    }

     @Bean
    public SimpleTriggerFactoryBean postScoreRefreshTrigger(JobDetail  postScoreRefreshJobDetail) {
        SimpleTriggerFactoryBean factoryBean = new SimpleTriggerFactoryBean();
        factoryBean.setJobDetail(postScoreRefreshJobDetail);
        factoryBean.setName("postScoreRefreshTrigger");
        factoryBean.setGroup("communityJobGroup");
        factoryBean.setRepeatInterval(1000*60*60*10);//多长时间执行一次任务
        factoryBean.setJobDataMap(new JobDataMap());//Trigger底层需要存储一些状态，新建JobDataMap对象来存储
        return factoryBean;
    }
}